
import BaseAd from "../../core/base/BaseAd";
import { SDKState } from "../../SDKDefine";
import { registerClass } from "../../../cfw/tools/class_decorator";

@registerClass('QQAppBoxAd')
export default class QQAppBoxAd extends BaseAd {
    hide(): void {
    }


    init() {
        this.preload();
    }

    // preload(s: SDKState) {
    //     this.setLogicState(s)
    //     console.log('QQAppBoxAd preload ====================')
    //     this.destroy()
    //     this.create()
    //     this.load()
    // }

    // open() {
    //     console.log('QQAppBoxAd open ')
    //     // this.destroy()

    //     if (this.state == SDKState.loadSucess) {
    //         this.show()
    //     } else {
    //         this.setLogicState(SDKState.open)
    //         this.load()
    //     }


    // }

    onClose() {
        console.log(' AppBox onClose ')
    }

    close(): void {
        this.load();
    }

    create() {
        if (!this.ad) {
            this.ad = this.sdk.createAppBox({ adUnitId: this.adUnitID })
            this.ad.onClose(this.getFunc(this.onClose))
        }
    }

    load() {

        console.log("QQAppBoxAd  load 1111")
        if (this.ad) {
            console.log("QQAppBoxAd  load ")
            this.ad.load().then(() => {
                console.log("QQAppBoxAd  load 2222 ")
                this.setState(SDKState.loadSucess)
                // this.show()
            }).catch((err: any) => {
                console.log("QQAppBoxAd  load err", err)
            })
        } else {
            console.log(' instance is null ')
        }
    }

    show() {
        console.log("QQAppBoxAd  show 1111")
        if (this.ad) {

            this.ad.show().then(() => {
                console.log("QQAppBoxAd  show ")
                // this.load()
            }).catch((err: any) => {
                console.log("QQAppBoxAd  show ", err)
                this.load()
            })
        }
    }

    destroy() {
        if (this.ad) {
            this.ad.offClose(this.getFunc(this.onClose))
            this.ad.destroy()
            this.ad = null;
        }
    }

}